package com.dxl.controller;

import com.dxl.entity.ResponseData;
import com.dxl.entity.SmsParams;
import com.dxl.utils.ResDataUtil;
import com.dxl.utils.TxCloudSmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.Random;

/**
 * @author dxl
 * @date 2020/12/18 23:28
 */
@Api(tags = "短信发送模块")
@RestController
@RequestMapping("/message")
@Validated
public class MessageController {
    @Autowired
    TxCloudSmsUtil txCloudSmsUtil;
    /**
     * 发送短信
     * @return
     */
    @ApiOperation(value = "发送短信")
    @ResponseBody
    @GetMapping("/sendTemplate")
    public ResponseData send(
            @ApiParam(value = "手机号",defaultValue = "16605118886")
            @RequestParam(value = "phone",required = false)
            @Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")
            @NotBlank(message = "手机号不能为空") String phone ){
        // 生成随机的六位数验证码
        Random random = new Random(4);
        Integer verifyCode = random.nextInt(1000000);
        SmsParams smsParams = new SmsParams(phone,verifyCode.toString());
        System.out.println("生成的验证码为：" + verifyCode);
        String str = txCloudSmsUtil.sendSms(smsParams);
        System.out.println("发送结果：" + str);
        if(str.equals("success")){
           return ResDataUtil.success("发送成功");
        }else{
            return ResDataUtil.failure("发送失败",str);
        }

    }
}
